﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Pipes;

namespace ComPowerSwitch
{
    static public class ComSwitch
    {
        static private NamedPipeClientStream connect()
        {
            

            NamedPipeClientStream nSendPipe;

            nSendPipe = new NamedPipeClientStream("\\\\.", "\\ComSwitchTray", PipeDirection.Out);
            nSendPipe.Connect(1000);


            return nSendPipe;
        }

        static public bool Reset()
        {
            try
            {
                byte[] cmdBuf = new byte[1];
                cmdBuf[0] = 0;
                NamedPipeClientStream nSendPipe = connect();
                nSendPipe.Write(cmdBuf, 0, 1);
                nSendPipe.Close();
            }
            catch
            {
                return false;
            }

            return true;
        }

        static public bool PowerOn()
        {
            try
            {
                byte[] cmdBuf = new byte[1];
                cmdBuf[0] = 1;
                NamedPipeClientStream nSendPipe = connect();
                nSendPipe.Write(cmdBuf, 0, 1);
                nSendPipe.Close();
            }
            catch
            {
                return false;
            }

            return true;
        }

        static public bool PowerOff()
        {
            try
            {
                byte[] cmdBuf = new byte[1];
                cmdBuf[0] = 2;
                NamedPipeClientStream nSendPipe = connect();
                nSendPipe.Write(cmdBuf, 0, 1);
                nSendPipe.Close();
            }
            catch
            {
                return false;
            }

            return true;
        }
    }
}
